define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Myagentsale, GoDialog, GoPage,$stateParams, $ionicSlideBoxDelegate, $window,$timeout) {
		
		$scope.selectIndex = 0;
		$scope.datasArr = new Array();//存放一览数据
		$scope.nPageArr = [1,1];//页数
		
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
			var all = $window.innerHeight;
			$scope.myObj = { //滚动条可视范围
				"height": all - 117 - 44*2 - lan + "px"
			};
			$scope.store_id = $stateParams.store_id;
			$scope.init_data();
		}, false);

		//初始化。0：当月，1：上月
		$scope.init_data = function() {
			Myagentsale.getList($scope.store_id,$scope.selectIndex,1,function(data){
				$scope.datasArr[$scope.selectIndex] = data;
//				console.log($scope.datasArr);
			})
		}
		
		//手动切换
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
			if(!$scope.datasArr[$scope.selectIndex]){
				$scope.init_data();
			}
		};
		//滑动时候触发
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('main-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('main-handle').enableSlide(true);
		};
		
		/*===================下拉刷新 =============*/
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Myagentsale.getList($scope.store_id, $scope.selectIndex, 1, function(data) {
				$scope.datasArr[$scope.selectIndex] = data;
				$scope.nPageArr[$scope.selectIndex] = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Myagentsale.getList($scope.store_id, $scope.selectIndex, $scope.nPageArr[$scope.selectIndex]+1,function(data) {
				console.log(data)
				if(data != null) {
					if($scope.datasArr[$scope.selectIndex] != undefined) {
						$scope.datasArr[$scope.selectIndex].order = $scope.datasArr[$scope.selectIndex].order.concat(data.order);
						$scope.nPageArr[$scope.selectIndex] += 1;
						console.log($scope.nPageArr)
					} else {
						$scope.datasArr[$scope.selectIndex] = data;
					}
				}
			},function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		/*==================下拉刷新 =============*/
		
		
	}

	controller.$inject = ['$scope', 'Myagentsale', 'GoDialog', 'GoPage','$stateParams','$ionicSlideBoxDelegate','$window','$timeout'];

	return app.registerController("myagentsalectrl",controller);
});